<?php

class Diner extends MY_Controller
{
	private $base_url;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('diner_model');
		$this->load->helper('url');
		$this->load->library('session');
		$this->base_url = array('base_url' => $this->config->item('base_url'));
		$this->assign('base_url', $this->base_url);
		$this->assign('links', NULL);
	}

	public function index($offset = 0)
	{
		if(isset($_SESSION['id']) && $_SESSION['type'] == 1){
			$result = $this->diner_model->get_merchant($offset);
			if ($result){
				$this->assign('merchants', $result);
				$this->assign('title', "订餐");
				$this->assign('links', array('css/style.css','css/css4.1/bootstrapcustom.min.css'));
				$this->display('diner/head.tpl');
				$this->display('diner/restaurant.tpl');
			}else{
				echo "<script>alert('没有更多商家');</script>";
			}
		} else{
			redirect('user/login');
		}
	}

	public function shop($mid)
	{
		if(isset($_SESSION['id']) && $_SESSION['type'] == 1){
			if (!array_key_exists($mid, $_SESSION['order'])){
				$_SESSION['order'][$mid] = array();
			}
			$result = $this->diner_model->getDishesByMid($mid);
			if ($result){
				$this->assign('mid', $mid);
				$this->assign('dishes', $result);
				$this->assign('order_info', json_encode($_SESSION['order'][$mid]));
				$this->assign('title', $this->diner_model->get_mname_by_mid($mid)->mname);
				$this->assign('links', array('shop/css/merchantPage.css'));
				$this->display('diner/head.tpl');
				$this->display('diner/shop/index.tpl');
			}else{
				echo "<script>alert('没有更多菜品');</script>";
			}
		} else{
			redirect('user/login');
		}
	}

	public function dish($mid)
	{
		if (isset($_SESSION['id']) && $_SESSION['type'] == 1){

			if (!empty($_POST['act'])){
				$result = $this->diner_model->getDishesByMid($mid);

				if($_POST['act'] === 'clean'){
					$_SESSION['order'][$mid] = array();
				} elseif (!empty($_POST['id'])) {
					if ($_POST['act'] === 'add') {
						if (array_key_exists($_POST['id'], $_SESSION['order'][$mid])) {
							$_SESSION['order'][$mid][$_POST['id']]['number'] += 1;
						} else {
							$_SESSION['order'][$mid][$_POST['id']] = array(
								"number" => 1,
								"name" => $result[$_POST['id']]['dname'],
								"price" => $result[$_POST['id']]['price'] * $result[$_POST['id']]['discount']
							);
						}
						echo json_encode($this->diner_model->getDishByDid($_POST['id']));
					} elseif ($_POST['act'] === 'min') {
						if (array_key_exists($_POST['id'], $_SESSION['order'][$mid])) {
							if ($_SESSION['order'][$mid][$_POST['id']]['number'] > 1)
								$_SESSION['order'][$mid][$_POST['id']]['number'] -= 1;
							elseif ($_SESSION['order'][$mid][$_POST['id']]['number'] == 1) {
								unset($_SESSION['order'][$mid][$_POST['id']]);
							}
						}
						echo json_encode($this->diner_model->getDishByDid($_POST['id']));
					} elseif ($_POST['act'] === 'set' && !empty($_POST['number'])) {
						echo json_encode($_SESSION['order'][$mid][$_POST['id']]);
						$_SESSION['order'][$mid][$_POST['id']]['number'] = (int)$_POST['number'];
					}
				}
			}
		}else{
			redirect('user/login');
		}
	}

	public function order($mid)
	{
		if(isset($_SESSION['id']) && $_SESSION['type'] == 1){
			if (!empty($_POST['submit']) && !empty($_POST['addr'])){
				if ($this->diner_model->submit_order_from_session($_POST['addr'], $mid)){
					$_SESSION['order'] = array();
					$this->display('success/order.tpl');
				}else{
					echo "<script>alert('下单失败，请重新尝试');</script><script>history.back();</script>";
				}
			}else{
				$orders = $this->diner_model->getDishesByOrder($_SESSION['order'][$mid]);
				$addrs = $this->diner_model->get_addr_by_did($_SESSION['id']);
				if ($orders){
					$money = 0;
					foreach ($orders as $dish){
						$money += $dish['price'] * $dish['discount'] * $dish['number'];
					}
					$this->assign('money', $money);
				}
				$this->assign('orders',$orders);
				$this->assign('addrs', $addrs);
				$this->assign('mid', $mid);
				$this->display('diner/order.tpl');
			}
		} else{
			redirect('user/login');
		}
	}

	public function addaddr($mid){
		if(isset($_SESSION['id']) && $_SESSION['type'] == 1){
			if (!empty($_POST['addr']) && !empty($_POST['rname']) && !empty($_POST['phone_number'])){
				if($this->diner_model->add_addr($_POST['addr'], $_POST['rname'], $_POST['phone_number'], $_SESSION['id'])){
					echo "<script>alert('新增地址成功');window.location.href = '/ps_project/diner/order/$mid';</script>";
				}
			}
			$this->assign('mid', $mid);
			$this->display('diner/addaddr.tpl');
		} else{
			redirect('user/login');
		}
	}

	public function myorder(){
		if(isset($_SESSION['id']) && $_SESSION['type'] == 1){
			$result1 = $this->diner_model->get_my_unreceived_order($_SESSION['id']);
			$result2 = $this->diner_model->get_my_received_order($_SESSION['id']);
			$sending = array();
			$sent = array();
			foreach ($result2 as $order){
				if($order->arrival_time == null){
					array_push($sending, $order);
				}else{
					array_push($sent, $order);
				}
			}
			$this->assign('unreceived_orders', $result1);
			$this->assign('sending_orders', $sending);
			$this->assign('sent_orders', $sent);
			$this->assign('title', "我的订单");
			$this->assign('links', array('css/css4.1/bootstrapcustom.min.css'));
			$this->display('diner/head.tpl');
			$this->display('diner/myorder.tpl');

		} else{
			redirect('user/login');
		}
	}

	public function register()
	{
		if ($_SERVER['REQUEST_METHOD'] === 'POST'){
			if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['phone_number'])){
				if (strlen($_POST['phone_number']) === 11){
					$username = $_POST['username'];
					$password = $_POST['password'];
					$phone_number = $_POST['phone_number'];
					$result = $this->diner_model->insert_diner($username, $password, $phone_number);
					if ($result === 1){
						redirect('user/login');
					}elseif ($result === -1){
						echo "<script>alert('用户名已被注册');</script><script>history.back();</script>";
					}elseif ($result === -2){
						echo "<script>alert('联系电话已被注册');</script><script>history.back();</script>";
					}
				}else{
					echo "<script>alert('电话格式有误');</script><script>history.back();</script>";
				}
			}else{
				echo "<script>alert('请完善信息');</script><script>history.back();</script>";
			}
		}else{
			redirect('user/login');
		}
	}
}
